package coder.taiji.test.springnetty.tcp.inbound;

import coder.taiji.test.springnetty.tcp.dto.TestProtocolDTO;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

import java.nio.charset.Charset;

/**
 * describe:处理的数据第一个字节0 为登录数据。1为上行数据 后面接 string
 *
 * @author helloworldyu
 * @date 2018/3/14
 */
public class SecondInboundChannle extends ChannelInboundHandlerAdapter {
	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		ByteBuf byteBuf = (ByteBuf) msg;
		TestProtocolDTO testProtocolDTO = new TestProtocolDTO();
		//===========获取标识===============
		byte aByte = byteBuf.getByte(0);
		//===========获取数据===============
		String info = byteBuf.getCharSequence(1, byteBuf.readableBytes() - 1, Charset.defaultCharset()).toString();
		//===========组装数据==============
		testProtocolDTO.setType((int) aByte);
		testProtocolDTO.setInfo(info);

		System.out.println("secondinbound 处理完数据之后记得 release()资源");
		//=====解析完之后 ByteBuf msg 没有意义,记得释放========
		ReferenceCountUtil.release(msg);
		ctx.fireChannelRead(testProtocolDTO);
	}
}